import { v4 as uuidv4 } from 'uuid';
// import Block from 'module/Block/index';
import Block, { BlockAttritubes } from '../../module/Block/index';
import { request, summary, body, path } from 'koa-swagger-decorator';
import { RouterContext } from 'koa-router';

const blockSchema = {
  title: {
    type: 'string',
    require: true,
    description: '文章标题',
  },
  coverImage: {
    type: 'string',
    description: '文章封面',
  },
  editorType: {
    type: 'number',
    require: true,
    description: '文章编辑类型(例如markdown或富文本)',
  },
  isTop: {
    type: 'number',
    description: '是否置顶(1开启, 0关闭)',
  },
  hasComment: {
    type: 'number',
    description: '是否开启评论(1开启, 0关闭)',
  },
  content: {
    type: 'string',
    require: true,
    description: '文章内容',
  },
  authorId: {
    type: 'string',
    require: true,
    description: '作者ID',
  },
  author: {
    type: 'string',
    require: true,
    description: '作者名字',
  },
  collectionId: {
    type: 'string',
    description: '分类ID',
  },
  collection: {
    type: 'string',
    description: '分类名称',
  },
  tagId: {
    type: 'string',
    description: '标签ID',
  },
  tag: {
    type: 'string',
    description: '标签名称',
  },
  remark_1: {
    type: 'string',
    description: '备用',
  },
  remark_2: {
    type: 'string',
    description: '备用',
  },
};
export default class BlockController {
  @request('post', '/block')
  @summary('新增博客')
  @body(blockSchema)
  static async create(ctx: RouterContext, next) {
    const attrs = ctx.request.body as BlockAttritubes;
    attrs.id = uuidv4();
    await new Block().create(attrs);
    await next();
  }

  @request('delete', '/block/{ids}')
  @summary('删除博客(批量)')
  @path({
    ids: { type: 'string', required: true, description: '博客ID' },
  })
  static async destory(ctx: RouterContext, next) {
    if (!ctx.params.ids) {
      // throw new ParamsError('参数错误');
    }
    const params = ctx.params.ids?.split(',');
    await new Block().destroy(params || []);
    await next();
  }
}
